package wqh.xiao_kong.cfs2000;

import cn.tpson.dti.common.*;
import cn.tpson.dti.core.IDataProcessor;
import cn.tpson.hk.core.HKDTServer;
import cn.tpson.hk.dto.UploadAlarmMessage;
import org.springframework.beans.factory.annotation.Autowired;
import wqh.domain.XkDevice;
import wqh.domain.XkDeviceAlarm;
import wqh.domain.XkSensorAlarm;
import wqh.global.GlobalID;
import wqh.global.GlobalXkDevice;
import wqh.service.XkDeviceAlarmService;
import wqh.service.XkDeviceService;
import wqh.service.XkSensorAlarmService;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by devts on 2016/12/11.
 */
public class CFS2000Data implements IDataProcessor {

    @Autowired
    private XkDeviceService xkDeviceService;

    @Autowired
    private XkDeviceAlarmService xkDeviceAlarmService;

    @Autowired
    private XkSensorAlarmService xkSensorAlarmService;


    public void recieve(Object o, String s) {

    }

    public void recieve(Object o, String s, int i) {

    }

    //Object o 是获取到的数据，String s 是IP，int i 是端口号，long l是用户信息传输装置的用户编码
    public void recieve(Object o, String s, int i, long l) {
       try{
           UpDownStruct upDownStruct = (UpDownStruct)o;
           Byte cmd = upDownStruct.getCmd();
           if(cmd == 25){
//               HeartBeatReqMessage heartBeatReqMessage = new HeartBeatReqMessage();
//               heartBeatReqMessage.setHostId(String.valueOf(l));
//               HKDTServer.getInstance().addMessage(heartBeatReqMessage);
            //   System.out.println("!!!!!!!!!!!!!!!!");
            //   System.out.println("消控心跳");
           //    System.out.println("!!!!!!!!!!!!!!!!");
           }
           if(cmd == 2){
               UploadAlarmMessage alarm = new UploadAlarmMessage();
               alarm.setHostId(String.valueOf(l));
               alarm.setDeviceAddress(String.valueOf(((Up2) o).getDevAddr()));
               alarm.setAlarmType("10000");
               alarm.setAlarmValue("0");
               String time = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
               alarm.setAlarmTime(time);
               HKDTServer.getInstance().addMessage(alarm);
               insertSQL_Sensor((Up2)o, s, i ,l);
           }
           if(cmd == 21){
               insertSQL_TransmissionDevice21((Up21)o, s, i, l,21);
           }
           if(cmd == 24){
               insertSQL_TransmissionDevice24((Up24)o, s, i, l,24);
           }
           if(cmd == 100){
               insertSQL_TransmissionDevice100((Up100)o, s, i, l,100);
           }
           if(cmd == 101){
               insertSQL_TransmissionDevice101((Up101)o, s, i, l,101);
           }
           if(cmd == 102){
               insertSQL_TransmissionDevice102((Up102)o, s, i, l,102);
           }
           if(cmd == 103){
               insertSQL_TransmissionDevice103((Up103)o, s, i, l,103);
           }
           if(cmd == 104){
               insertSQL_TransmissionDevice104((Up104)o, s, i, l,104);
           }
       }catch (Exception e){
           e.printStackTrace();
       }
    }

    public void onLine(String s, boolean b) {

    }

    public void onLine(String s, int i, boolean b) {

    }

    public void onLine(String s, int i, long l, boolean b) {
        if(b){
            GlobalID.addID(String.valueOf(l));
            GlobalXkDevice.addDevice(l);
            XkDevice xkDevice = xkDeviceService.findByDeviceID(l);
            if(xkDevice == null){
                XkDevice x = new XkDevice();
                x.setIp(s);
                x.setDevice_id(l);
                xkDeviceService.save(x);
            }else{
                if(!xkDevice.getIp().equals(s)){
                    xkDeviceService.updateIpByDeviceID(l,s);
                }
            }
        }else{
            GlobalXkDevice.removeDevice(l);
            GlobalID.removeID(String.valueOf(l));
        }
    }

    public void exception(org.apache.mina.core.session.IoSession ioSession, Throwable throwable) {
        System.out.println("cfs2000 error: " + throwable.getMessage());
        throwable.printStackTrace();
    }

    private void insertSQL_Sensor(Up2 data, String ip, int port, long code){
        XkSensorAlarm s = new XkSensorAlarm();
        s.setDevice_id(code);
        s.setIp(ip);
        s.setSensor_id(data.getDevAddr());
        if(data.getType() == 1) s.setStatus(UpAnalysis.getSensorStatusChineseSystemType_1(data.getDevStatus()));
        if(data.getType() == 100) s.setStatus(UpAnalysis.getSensorStatusChineseSystemType_100(data.getDevStatus()));
        if(data.getType() == 101) s.setStatus(UpAnalysis.getSensorStatusChineseSystemType_101(data.getDevStatus()));
        if(data.getType() == 102) s.setStatus(UpAnalysis.getSensorStatusChineseSystemType_102(data.getDevStatus()));
        s.setAlarm_time(new Long(System.currentTimeMillis() / 1000).intValue());
        try {
            xkSensorAlarmService.save(s);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void insertSQL_TransmissionDevice24(Up24 data, String ip, int port, long code, Integer protocol){
        XkDeviceAlarm t = new XkDeviceAlarm();
        t.setIp(ip);
        t.setDevice_id(code);
        if(data.toString() == null) t.setStatus("未知类型，协议："  + protocol);
        else t.setStatus(data.toString());
        t.setAlarm_time(new Long(System.currentTimeMillis() / 1000).intValue());
        t.setProtocol(protocol);
        try {
            xkDeviceAlarmService.save(t);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void insertSQL_TransmissionDevice21(Up21 data, String ip, int port, long code, Integer protocol){
        XkDeviceAlarm t = new XkDeviceAlarm();
        t.setIp(ip);
        t.setDevice_id(code);
        if(data.toString() == null) t.setStatus("未知类型，协议："  + protocol);
        else t.setStatus(data.toString());
        t.setAlarm_time(new Long(System.currentTimeMillis() / 1000).intValue());
        t.setProtocol(protocol);
        try {
            xkDeviceAlarmService.save(t);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void insertSQL_TransmissionDevice100(Up100 data, String ip, int port, long code, Integer protocol){
        XkDeviceAlarm t = new XkDeviceAlarm();
        t.setIp(ip);
        t.setDevice_id(code);
        if(data.toString() == null) t.setStatus("未知类型，协议："  + protocol);
        else t.setStatus(data.toString());
        t.setAlarm_time(new Long(System.currentTimeMillis() / 1000).intValue());
        t.setProtocol(protocol);
        try {
            xkDeviceAlarmService.save(t);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void insertSQL_TransmissionDevice101(Up101 data, String ip, int port, long code, Integer protocol){
        XkDeviceAlarm t = new XkDeviceAlarm();
        t.setIp(ip);
        t.setDevice_id(code);
        if(data.toString() == null) t.setStatus("未知类型，协议："  + protocol);
        else t.setStatus(data.toString());
        t.setAlarm_time(new Long(System.currentTimeMillis() / 1000).intValue());
        t.setProtocol(protocol);
        try {
            xkDeviceAlarmService.save(t);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void insertSQL_TransmissionDevice102(Up102 data, String ip, int port, long code, Integer protocol){
        XkDeviceAlarm t = new XkDeviceAlarm();
        t.setIp(ip);
        t.setDevice_id(code);
        if(data.toString() == null) t.setStatus("未知类型，协议："  + protocol);
        else t.setStatus(data.toString());
        t.setAlarm_time(new Long(System.currentTimeMillis() / 1000).intValue());
        t.setProtocol(protocol);
        try {
            xkDeviceAlarmService.save(t);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void insertSQL_TransmissionDevice103(Up103 data, String ip, int port, long code, Integer protocol){
        XkDeviceAlarm t = new XkDeviceAlarm();
        t.setIp(ip);
        t.setDevice_id(code);
        if(data.toString() == null) t.setStatus("未知类型，协议："  + protocol);
        else t.setStatus(data.toString());
        t.setAlarm_time(new Long(System.currentTimeMillis() / 1000).intValue());
        t.setProtocol(protocol);
        try {
            xkDeviceAlarmService.save(t);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void insertSQL_TransmissionDevice104(Up104 data, String ip, int port, long code, Integer protocol){
        XkDeviceAlarm t = new XkDeviceAlarm();
        t.setIp(ip);
        t.setDevice_id(code);
        if(data.toString() == null) t.setStatus("未知类型，协议："  + protocol);
        else t.setStatus(data.toString());
        t.setAlarm_time(new Long(System.currentTimeMillis() / 1000).intValue());
        t.setProtocol(protocol);
        try {
            xkDeviceAlarmService.save(t);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
